#/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2022/1/11 11:04
# @Author  : wyy
# @Email   : 1915059545@qq.com
# @File    : page_ht_home.py
# @Software: PyCharm
"""合同主页"""
from selenium.webdriver.common.by import By
from pages.base_page import BasePage

class PageHeTongHome(BasePage):

    hetong_locator = (By.LINK_TEXT, "合同")
    tianjiahetong_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div:nth-child(1) > div > a")
    shanchu_locator = (By.CSS_SELECTOR, "#delete")
    bianji_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[2]/td[10]/a[2]')
    xhetong_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[1]/td[1]/input')
    # 来源商机,负责人
    shangjilaiyuan_locator = (By.NAME, "business_name")
    fuzeren_locator = (By.ID, "owner_name")
    # 保存按钮
    baocun_locator = (By.CSS_SELECTOR,
                      "body > div.container > div.row > div > form > table > tfoot > tr > td:nth-child(2) > input.btn.btn-primary")
    # 勾选商机来源
    gouxuan_locator = (By.XPATH, '//*[@id="data"]/tr[1]/td[1]/input[1]')
    # 确认勾选商机的【确认】
    ok_locator = (By.XPATH, '/html/body/div[7]/div[3]/div/button[1]')
    # 勾选负责人
    gouxuanfuzeren_locator = (By.XPATH, '//*[@id="d_content"]/tr[1]/td[1]/input')
    # 确认勾选负责人的【确认】
    ok1_locator = (By.XPATH, '/html/body/div[8]/div[3]/div/button[1]')
    def click_hetong(self):
        self.driver.find_element(*self.hetong_locator).click()
    #点击添加合同按钮
    def click_tjht(self):
        self.driver.find_element(*self.tianjiahetong_locator).click()
    #点击删除
    def click_shanchu(self):
        self.driver.find_element(*self.shanchu_locator).click()
        self.driver.switch_to.alert.text
        self.driver.switch_to.alert.accept()
    #点击编辑,保存
    def click_bianji(self):
        self.driver.find_element(*self.bianji_locator).click()
    #保存编辑
    def bianji_baocun(self):
        baocun_locator=(By.XPATH,'//*[@id="tab1"]/form/table/thead/tr/td[2]/input[1]')
        self.driver.find_element(*baocun_locator).click()
    #编辑断言
    def bjht_duanyan(self):
        dy_locator1=(By.CSS_SELECTOR,"#tab1 > div.back_box > div")
        return self.driver.find_element(*dy_locator1).text
    #选择来源商机，选择商机
    def input_lysj(self):
        self.driver.find_element(*self.shangjilaiyuan_locator).click()
        self.driver.find_element(*self.gouxuan_locator).click()
        self.driver.find_element(*self.ok_locator).click()
    #选择负责人
    def input_fzr(self):
        self.driver.find_element(*self.fuzeren_locator).click()
        self.driver.find_element(*self.gouxuanfuzeren_locator).click()
        self.driver.find_element(*self.ok1_locator).click()


    #添加断言
    def tjht_duanyan(self):
        dy_locator1=(By.XPATH,'// *[ @ id = "form1"] / table / tbody / tr[1] / td[3] / a')
        return self.driver.find_element(*dy_locator1).text
#删除断言
    def scht_duanyan(self):
        dy_locator1=(By.CSS_SELECTOR,"body > div.container > div.alert.alert-success > button")
        return self.driver.find_element(*dy_locator1).text
    #勾选删除合同
    def delete_ht(self,htmc):
        ht_table_loccator = (By.XPATH, '//*[@id="form1"]/table/tbody')
        table = self.driver.find_element(*ht_table_loccator)
        trs = table.find_elements(By.TAG_NAME, "tr")
        for i in trs:
            tds = i.find_elements(By.TAG_NAME, "td")
            print(tds[2].text)
            if tds[2].text == (htmc):
                tds[0].find_element(By.TAG_NAME, "input").click()
                break

    #清理数据
    def clear(self,name):
        self.delete_ht(name)
        self.click_shanchu()
    #点击进入合同主页，点击进入添加合同
    def jr_tj_ht(self):
        self.click_hetong()
        self.click_tjht()

    #点击进入合同主页，点击进入编辑合同
    def jr_bj_ht(self):
        self.click_hetong()
        self.click_bianji()

    #编辑合同
    def bianjihetong(self):
        self.input_lysj()
        self.input_fzr()
        self.bianji_baocun()